package com.qixiucoding;

public class EnumDemo {
    public static void main(String[] args) {
        //创建枚举数组
        Day[] days = new Day[]{Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY,
                Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};
        for (int i = 0; i < days.length; i++) {
            System.out.println("day[" + i + "].ordinal():" + days[i].ordinal());
        }

        System.out.println("-------------------------------------");
        //通过compareTo方法比较,实际上其内部是通过ordinal()值比较的
        System.out.println("days[0].compareTo(days[1]):" + days[0].compareTo(days[1]));
        System.out.println("days[0].compareTo(days[2]):" + days[0].compareTo(days[2]));

        //获取该枚举对象的Class对象引用,当然也可以通过getClass方法
        Class<?> clazz = days[0].getDeclaringClass();
        System.out.println("clazz:" + clazz);

        System.out.println("-------------------------------------");

        //name()
        System.out.println("days[0].name():" + days[0].name());
        System.out.println("days[1].name():" + days[1].name());

        System.out.println("-------------------------------------");

        System.out.println("days[0].toString():" + days[0].toString());
        System.out.println("days[1].toString():" + days[1].toString());

        System.out.println("-------------------------------------");

        Day d = Enum.valueOf(Day.class, days[0].name());
        Day d2 = Day.valueOf(Day.class, days[0].name());
        System.out.println("d:" + d);
        System.out.println("d2:" + d2);
        Day day = Day.valueOf("MONDAY");
        System.out.println(day.getCode()+day.getDesc()+day);
    }
    enum Day {
        MONDAY("星期一",1),
        TUESDAY("星期二",2),
        WEDNESDAY("星期三",3),
        THURSDAY("星期四",4),
        FRIDAY("星期五",5),
        SATURDAY("星期六",6),
        SUNDAY("星期日",7);//记住要用分号结束


        private final String desc;
        private final int code;

        Day(String desc, int code) {
            this.desc = desc;
            this.code = code;
        }
        /**
         * 定义方法,返回描述,跟常规类的定义没区别
         * @return
         */
        public String getDesc(){
            return desc;
        }

        /**
         * 定义方法,返回代码,跟常规类的定义没区别
         * @return
         */
        public int getCode(){
            return code;
        }
    }

}
